﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using VideoReflector.Models;

namespace VideoReflector.Converters
{
    /// <summary>
    /// Enumeration of the possible tag group properties which can be returned by this converter
    /// </summary>
    public enum ClipTagGroupProperty
    {
        Label,
        Colour,
        Symbol
    }

    /// <summary>
    /// ValueConverter which takes:
    ///   * a clip tag group ID as first value
    ///   * collection of clip tag groups as second value
    ///   * one of the ClipTagGroupProperty enum values as the parameter
    /// and returns the specified property of the clip tag group with the specified ID
    /// </summary>
    [ValueConversion(typeof(int), typeof(string))]
    public class TagGroupIdLookupConverter : ConverterWithMarkupExtension, IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                var tagGroupId = (int)values[0];
                var tagGroups = (ObservableCollection<ClipTagGroup>)values[1];
                if (tagGroupId >= tagGroups.Count)
                    throw new ApplicationException("Supplied tag group ID must be within range of ClipTagGroups collection when using TagGroupIdLookupConverter");
                var tagPropertyString = (string)parameter;
                ClipTagGroupProperty tagProperty;
                if (!Enum.TryParse<ClipTagGroupProperty>(tagPropertyString, out tagProperty))
                    throw new ApplicationException("Parameter must be one of ClipTagGroupProperty values (as string) when using TagGroupIdLookupConverter");
                switch (tagProperty)
                {
                    case ClipTagGroupProperty.Colour:
                        var brushConv = new System.Windows.Media.BrushConverter();
                        return brushConv.ConvertFrom(tagGroups[tagGroupId].Colour);
                    case ClipTagGroupProperty.Symbol: 
                        return Application.Current.Resources[tagGroups[tagGroupId].Symbol.Item];
                    default:
                    case ClipTagGroupProperty.Label:  
                        return tagGroups[tagGroupId].Label;
                }
            }
            catch (InvalidCastException)
            {
                /*
                throw new ApplicationException(
                    String.Format("First value must be of type int ({0}), second value a collection of ClipTagGroup ({1})"
                                  + " and parameter of type string ({2}) when using TagGroupIdLookupConverter", 
                                  values[0].GetType(), values[1].GetType(), parameter.GetType()), e);
                 */
                return Binding.DoNothing;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
